package com.car.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.account.client.ServeClient;
import com.car.account.client.dto.CarTypeDto;
import com.car.account.dto.userCarDto.UserCarDto;
import com.car.account.entity.User;
import com.car.account.entity.UserCar;
import com.car.account.exception.AccountException;
import com.car.account.mapper.UserCarMapper;
import com.car.account.param.userCarParam.UserBindCarParam;
import com.car.account.service.UserCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.account.service.UserService;
import com.car.account.util.ListUtil;
import com.car.account.util.validatorUtil.CarIdValidator;
import com.car.commons.core.model.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class UserCarServiceImpl extends ServiceImpl<UserCarMapper, UserCar> implements UserCarService {

    @Resource
    private UserService userService;

    @Resource
    private ServeClient serveClient;

    @Override
    @Transactional
    public void userBindCar(UserBindCarParam param) throws Exception {
        //判断车牌号格式
        if (!CarIdValidator.isValidLicensePlate(param.getCarNumber()))
            throw new AccountException(4001,"车牌号格式错误");
        //判断是否存在用户
        User user = userService.getById(param.getUserId());
        if (user == null || user.getUserState() == 2) throw new AccountException(1000,"用户不存在");
        //判断用户状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断车辆类型
        Result<CarTypeDto> result = serveClient.selectCarTypeById(param.getCarTypeId());
        if (result.getCode() != 200)
            throw new AccountException(result.getCode(),result.getMsg());
        //判断绑定车辆数
        List<UserCar> userCars = list(new QueryWrapper<UserCar>().eq("user_id", param.getUserId()));
        if (userCars.size() >= 5) throw new AccountException(4002,"最多绑定5辆车");
        //增加用户车辆
        UserCar userCar = new UserCar();
        userCar.setCarName(param.getCarName());
        userCar.setCarNumber(param.getCarNumber());
        userCar.setImg(param.getImg());
        userCar.setUserId(param.getUserId());
        userCar.setCarTypeId(param.getCarTypeId());
        userCar.setCarDescribe(param.getCarDescribe());
        save(userCar);
    }

    @Override
    @Transactional
    public void userUnbindingCar(Integer userId, Integer userCarId) throws Exception {
        //判断是否存在用户
        User user = userService.getById(userId);
        if (user == null || user.getUserState() == 2) throw new AccountException(1000,"用户不存在");
        //判断用户状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断是否存在绑定车辆
        UserCar userCar = getById(userCarId);
        if (userCar == null) throw new AccountException(4001,"不存在绑定车辆");
        //判断是否属于当前用户
        if (userCar.getUserId() != userId) throw new AccountException(4002,"用户与车辆信息不匹配");
        //删除绑定信息
        removeById(userCarId);
    }

    @Override
    public List<UserCarDto> selectCarByUserId(Integer userId) throws Exception {
        //判断是否存在用户
        User user = userService.getById(userId);
        if (user == null || user.getUserState() == 2) throw new AccountException(1000,"用户不存在");
        //判断用户状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //根据条件查询车辆
        List<UserCar> userCars = list(new QueryWrapper<UserCar>().eq("user_id", userId));
        return ListUtil.copyToList(userCars, UserCarDto.class);
    }
}
